package com.console.operations.loanmgr;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

import com.common.test.GetDataFromImportFile;
import com.common.test.OperationBase;
import com.common.test.UITestBase;
import com.common.util.DBUtil;
import com.common.util.GetTempleteFileUtil;
import com.console.pages.loanmgr.LoanBatchPage;

	/*
	@author 王嘉犇
	@name 多条单标债券批量导入
	@type 动作类
	 */

public class LoanBatch extends OperationBase {

	public LoanBatch(UITestBase tb){
		super(tb);
	}
	
	//导入的理财计划单标状态是否都为审核中
	public boolean isallstatuscorrected = true;
	
	public void batchoperation() throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        Calendar cal = Calendar.getInstance();
        Date date = cal.getTime();
        String datestr = sdf.format(date);
        Random random = new Random();
        int x = random.nextInt(8999);
        x = x+1000;
        String tempstr = datestr + String.valueOf(x);
        //得到excel里某列标题里的借款标题单元格数据，放进一个list里
        List<String> loantitlelst = GetDataFromImportFile.getMultiLineDataListFromIndex(GetTempleteFileUtil.GRAB_FOREMAN_LOAN, "借款标题");
		for(int i=0;i<loantitlelst.size();i++){
			String loantitle = loantitlelst.get(i);
			//如果借款标题存在，则更新
			if(DBUtil.isQueryData("select * from fiz_loan where vc_name = ? and dc_type = ?", new Object[]{loantitle, "00"})){
				DBUtil.update("update fiz_loan set vc_name = ? where vc_name = ? and dc_type = ?", new Object[]{tempstr + "000" + (i+1), loantitle, "00"});
			}
		}
		//得到excel里某列标题里的合同编号单元格数据，放进一个list里
		List<String> contractnolst = GetDataFromImportFile.getMultiLineDataListFromIndex(GetTempleteFileUtil.GRAB_FOREMAN_LOAN, "合同编号");
		for(int i=0;i<contractnolst.size();i++){
			String contractno = contractnolst.get(i);
			//如果fiz_loan_prop表里存在vc_code为contract-no且vc_value为导入的合同编号时，把合同编号更新为固定时间格式
			if(DBUtil.isQueryData("select * from fiz_loan_prop where vc_code = 'contract-no' and vc_value = ?", new Object[]{contractno})){
				DBUtil.update("update fiz_loan_prop set vc_value = ? where vc_code = 'contract-no' and vc_value = ?", new Object[]{tempstr + "000" + (i+1), contractno});
			}
		}
		switchToDefaultContent();
		LoanBatchPage lbp = new LoanBatchPage(tb);
		lbp.upLoadFile.chooseFile(GetTempleteFileUtil.GRAB_FOREMAN_LOAN);
		pause(1000);
		lbp.upLoadButton.click();
		waitForElementExist(lbp.importButton);
		//点击导入按钮
		lbp.importButton.click();
		pause(4000);
		Set<String> statusset = new HashSet<String>();
		for(int i=0;i<contractnolst.size();i++){
			String contractno = contractnolst.get(i);
			String status = DBUtil.queryOneCell("select dc_status\n" +
											    "from fiz_loan\n" +
												"where pk_id = (\n" +
											    			   "select fk_loan_id\n" +
											    			   "from fiz_loan_prop\n" +
											    			   "where vc_code = ?\n" +
											    			     "and vc_value = ?\n" +
											    			  ")\n" +
											      "and dc_type = ?", new Object[]{"contract-no", contractno, "00"});
			if(!status.equals("00")){
				log.error("单标合同号 " + contractno + "对应债券状态不正确");
			}
			statusset.add(status);
		}
		//判断导入的理财计划单标状态是否都为审核中
		isallstatuscorrected = statusset.size()==1 && statusset.contains("00");
	}
}
